package com.joolun.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.joolun.common.corp.TenantContext;
import com.joolun.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import com.joolun.system.mapper.SysCorpConfigMapper;
import com.joolun.system.domain.SysCorpConfig;
import com.joolun.system.service.ISysCorpConfigService;

/**
 * 企业配置Service业务层处理
 * 
 * @author joolun
 * @date 2025-06-19
 */
@Service
public class SysCorpConfigServiceImpl extends  ServiceImpl<SysCorpConfigMapper, SysCorpConfig> implements ISysCorpConfigService
{
    @Autowired
    private SysCorpConfigMapper sysCorpConfigMapper;


    // 缓存配置，避免频繁查询数据库
    private final LoadingCache<String, SysCorpConfig> configCache = Caffeine.newBuilder()
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build(this::loadConfigByCorpId);

    /**
     * 加载企业配置
     */
    private SysCorpConfig loadConfigByCorpId(String corpId) {
        return sysCorpConfigMapper.selectOne(
                new QueryWrapper<SysCorpConfig>().eq("corp_id", corpId != null && corpId != "" ? corpId : TenantContext.getTenantId())
        );
    }

    /**
     * 获取企业配置
     */
    @Override
    public SysCorpConfig getConfig(String corpId) {
        return configCache.get(corpId);
    }

    /**
     * 切换企业配置
     */
    @Override
    public void updateConfig(String corpId) {
        // String corpId, SysCorpConfig config
//        config.setCorpId(corpId);
//        config.setUpdateTime(new Date());
//
//        if (config.getId() == null) {
//            config.setCreateTime(new Date());
//            sysCorpConfigMapper.insert(config);
//        } else {
//            sysCorpConfigMapper.updateById(config);
//        }

        configCache.invalidate(corpId); // 刷新缓存
    }













    /**
     * 查询企业配置
     * 
     * @param id 企业配置主键
     * @return 企业配置
     */
    @Override
    public SysCorpConfig selectSysCorpConfigById(Long id)
    {
        return sysCorpConfigMapper.selectSysCorpConfigById(id);
    }


    /**
     * 查询企业配置列表
     * 
     * @param
     * @return 企业配置
     */
    @Override
    public List<SysCorpConfig> selectSysCorpConfigList()
    {
        return sysCorpConfigMapper.selectSysCorpConfigList();
    }

    /**
     * 新增企业配置
     * 
     * @param sysCorpConfig 企业配置
     * @return 结果
     */
    @Override
    public int insertSysCorpConfig(SysCorpConfig sysCorpConfig)
    {
        sysCorpConfig.setCreateTime(DateUtils.getNowDate());
        return sysCorpConfigMapper.insertSysCorpConfig(sysCorpConfig);
    }

    /**
     * 修改企业配置
     * 
     * @param sysCorpConfig 企业配置
     * @return 结果
     */
    @Override
    public int updateSysCorpConfig(SysCorpConfig sysCorpConfig)
    {
        sysCorpConfig.setUpdateTime(DateUtils.getNowDate());
        return sysCorpConfigMapper.updateSysCorpConfig(sysCorpConfig);
    }

    /**
     * 批量删除企业配置
     * 
     * @param ids 需要删除的企业配置主键
     * @return 结果
     */
    @Override
    public int deleteSysCorpConfigByIds(Long[] ids)
    {
        return sysCorpConfigMapper.deleteSysCorpConfigByIds(ids);
    }

    /**
     * 删除企业配置信息
     * 
     * @param id 企业配置主键
     * @return 结果
     */
    @Override
    public int deleteSysCorpConfigById(Long id)
    {
        return sysCorpConfigMapper.deleteSysCorpConfigById(id);
    }
}
